package com.turbo.cloud.ums.utils.vuemenu;

import com.turbo.cloud.ums.web.model.menu.UmsMenuVo;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zhangluning
 */
public class VueMenuUtil {

    /**
     * 菜单转Vue使用的树List
     */
    public static List<VueMenu> toVueMenuTreeList(List<UmsMenuVo> umsMenuVoList) {
        List<VueMenu> list = umsMenuVoList.stream().map(VueMenuUtil::toVueMenu).toList();
        return toTreeList(list);
    }

    /**
     * 菜单转Vue使用的树
     */
    private static VueMenu toVueMenu(UmsMenuVo umsMenuVo) {
        VueMenu menu = new VueMenu();
        menu.setId(umsMenuVo.getId());
        menu.setParentId(umsMenuVo.getParentId());
        menu.setPath(umsMenuVo.getRouterPath());
        menu.setName(umsMenuVo.getRouterName());
        menu.setComponent(umsMenuVo.getComponentPath());
        menu.setRedirect(umsMenuVo.getRedirect());

        VueMenuMeta meta = new VueMenuMeta();
        meta.setTitle(umsMenuVo.getMenuTitle());
        meta.setHideMenu(Integer.valueOf(1).equals(umsMenuVo.getHideInMenuId()));
        meta.setHideInMenu(Integer.valueOf(1).equals(umsMenuVo.getHideInMenuId()));
        meta.setHideTab(Integer.valueOf(1).equals(umsMenuVo.getHideTabId()));
        meta.setHideBreadcrumb(Integer.valueOf(1).equals(umsMenuVo.getHideBreadcrumbId()));
        meta.setHideChildrenInMenu(Integer.valueOf(1).equals(umsMenuVo.getHideChildrenInMenuId()));
        meta.setIgnoreKeepAlive(Integer.valueOf(0).equals(umsMenuVo.getKeepAliveId()));
        meta.setIcon(umsMenuVo.getIcon());
        menu.setMeta(meta);
        return menu;
    }

    /**
     * 菜单转Vue使用的树List
     */
    public static List<VueMenu> toSimpleVueMenuList(List<UmsMenuVo> umsMenuVoList) {
        List<VueMenu> list = umsMenuVoList.stream().map(VueMenuUtil::toSimpleVueMenu).toList();
        return list;
    }

    /**
     * 菜单转Vue使用的树
     */
    private static VueMenu toSimpleVueMenu(UmsMenuVo umsMenuVo) {
        VueMenu menu = new VueMenu();
        menu.setId(umsMenuVo.getId());
        menu.setParentId(umsMenuVo.getParentId());
        menu.setTitle(umsMenuVo.getMenuTitle());
        menu.setIcon(umsMenuVo.getIcon());

        // 是否外部链接：0->否；1->是
        if(Integer.valueOf(0).equals(umsMenuVo.getIsExtId())) {
            menu.setPath(umsMenuVo.getRouterPath());
        } else {
            menu.setUrl(umsMenuVo.getRouterPath());
        }

        menu.setName(umsMenuVo.getRouterName());
        menu.setComponent(umsMenuVo.getComponentPath());
        menu.setRedirect(umsMenuVo.getRedirect());
        menu.setKeepAlive(Integer.valueOf(0).equals(umsMenuVo.getKeepAliveId()));
        menu.setHideInMenu(Integer.valueOf(0).equals(umsMenuVo.getHideInMenuId()));
        menu.setHideBreadcrumb(Integer.valueOf(0).equals(umsMenuVo.getHideBreadcrumbId()));
        menu.setHideChildrenInMenu(Integer.valueOf(0).equals(umsMenuVo.getHideChildrenInMenuId()));

        return menu;
    }

    /**
     * 两层循环实现建树
     * @param treeNodes 传入的树节点列表
     * @return
     */
    private static List<VueMenu> toTreeList(List<VueMenu> treeNodes) {
        List<VueMenu> trees = new ArrayList<>();
        for (VueMenu treeNode : treeNodes) {
            if ("0".equals(treeNode.getParentId())) {
                trees.add(treeNode);
            }
            for (VueMenu it : treeNodes) {
                if (treeNode.getId().equals(it.getParentId())) {
                    if (treeNode.getChildren() == null) {
                        treeNode.setChildren(new ArrayList<>());
                    }
                    treeNode.getChildren().add(it);
                }
            }
        }
        return trees;
    }
}
